package com.huilai.platform.country_platform.repository;

import com.huilai.platform.country_platform.model.User;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;

/**
 * 作者：洪霓
 * 日期：2023/2/13
 * 注释：
 */
@Repository
    public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecificationExecutor<User> {
    User findUserByUsername(String username);

    User findUserById(Integer id);

//    @Query(value = "select * from User u", nativeQuery = true)
//    Page<User> findAllUser();

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query(value = "update User u set u.nickname = :#{#user.nickname}, " +
            "u.postcard = :#{#user.postcard}, " +
            "u.sex = :#{#user.sex}, " +
            "u.telephone = :#{#user.telephone}," +
            "u.education = :#{#user.education}," +
            "u.position = :#{#user.position}, " +
            "u.political = :#{#user.political}, " +
            "u.town = :#{#user.town}," +
            "u.village = :#{#user.village}," +
            "u.address = :#{#user.address} where u.id = :#{#user.id}",nativeQuery = true)
    Integer updateUser(User user);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query(value = "update User u set u.password = :#{#user.password} where u.id = :#{#user.id}",nativeQuery = true)
    Integer updateUserPassword(User user);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query("delete from User u where u.id = :id")
    Integer deleteUserById(Integer id);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query(value = "insert into User(nickname, username, password, postcard, sex, telephone, education, position, political, town, village, address)" +
            "values(:#{#user.nickname},:#{#user.username},:#{#user.password},:#{#user.postcard},:#{#user.sex},:#{#user.telephone},:#{#user.education},:#{#user.position},:#{#user.political},:#{#user.town},:#{#user.village},:#{#user.address})", nativeQuery = true)
    Integer addUser(User user);
}